<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
      <el-form-item label="员工工号" prop="workNumber">
        <el-input v-model="dataForm.workNumber" placeholder="员工工号" @change="getUserInfoByWorkNumber"></el-input>
      </el-form-item>
      <el-form-item label="员工姓名" prop="realName">
        <el-input v-model="dataForm.realName" placeholder="员工姓名" disabled></el-input>
      </el-form-item>
      <el-form-item label="所属角色" prop="roleNames">
        <el-input v-model="dataForm.roleNames" placeholder="所属角色" disabled></el-input>
      </el-form-item>
      <el-form-item label="折扣(0-1)" prop="discount">
        <el-input v-model="dataForm.discount" placeholder="折扣(0-1)"></el-input>
      </el-form-item>
      <el-form-item label="特殊折扣次数" prop="specialDiscountNum">
        <el-input v-model="dataForm.specialDiscountNum" placeholder="特殊折扣次数"></el-input>
      </el-form-item>
      <el-form-item label="特殊折扣(0-1)" prop="specialDiscount">
        <el-input v-model="dataForm.specialDiscount" placeholder="特殊折扣(0-1)"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-prevent-re-click>确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {
    getObj,
    addObj,
    putObj,
    getUsernameAndRoleName,
    getInfo
  } from "@/api/admin/sysempdiscountpermissions"
  import { getUserInfo } from '@/api/admin/user.js'

  export default {
    data() {
      var valueCheck = (rule, value, callback) => {
        if (value > 1) {
          callback(new Error("请输入0-1之间含两位小数的值"));
        } else if (value < 0) {
          callback(new Error("请输入0-1之间含两位小数的值!"));
        } else {
          callback();
        }
      };

      return {
        visible: false,
        dataForm: {
          id: 0,
          userId: "",
          workNumber: "",
          username: "",
          roleNames: "",
          discount: "",
          specialDiscountNum: "",
          specialDiscount: ""
        },
        dataRule: {
          workNumber: [{
            required: true,
            message: "员工id不能为空",
            trigger: "blur"
          }],
          discount: [{
              required: true,
              message: "请输入0-1之间含两位小数的值"
            },
            {
              validator: valueCheck
            }
          ],
          specialDiscountNum: [{
            required: true,
            message: "特殊折扣次数不能为空"
          }],
          specialDiscount: [{
              required: true,
              message: "特殊折扣(0-1)不能为空"
            },
            {
              validator: valueCheck
            }
          ]
        }
      };
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs["dataForm"].resetFields()
          if (this.dataForm.id) {
            getObj(this.dataForm.id).then(response => {
              if(response.data.code == '0'){
                this.dataForm = response.data.data
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      },
      getUserInfoByWorkNumber(val) {
        this.dataForm.workNumber
        getUsernameAndRoleName(val).then(response => {
          if(response.data.code == '0'){
            this.dataForm = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs["dataForm"].validate(valid => {
          if (valid) {
            if (this.dataForm.id) {
              putObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success("修改成功")
                  this.visible = false
                  this.$emit("refreshDataList")
                }else{
                  this.$message.error(response.data.msg)
                }
              })
            } else {
              addObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success("添加成功")
                  this.visible = false
                  this.$emit("refreshDataList")
                }else{
                  this.$message.error(response.data.msg)
                }
              })
            }
          }
        })
      }
    }
  };
</script>
